package com.hly.tool.project;

import com.hly.tool.constants.Constants;
import com.hly.tool.constants.HConstants;
import com.hly.tool.relationship.RelationshipUtil;
import com.matrixone.apps.domain.DomainConstants;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.DomainRelationship;
import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.framework.ui.UIUtil;
import com.matrixone.apps.program.ProjectSpace;
import matrix.db.Context;
import matrix.db.JPO;
import matrix.util.MatrixException;
import matrix.util.StringList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/10/19 13:40
 * @description：
 */
public class ProjectSpacesUtil
{

    /**
     * 获取项目空间下的成员
     *
     * @param context
     * @param projectId
     * @return
     * @throws MatrixException
     */
    public static MapList getProjectMember(Context context, String projectId) throws MatrixException
    {
        List<String> relationshipAttrNames = RelationshipUtil.getRelationshipAttrNames(context, Constants.rel_Member);
        StringList rellist = new StringList();
        for (int i = 0; i < relationshipAttrNames.size(); i++)
        {
            String attrName = relationshipAttrNames.get(i);
            rellist.add("attribute[" + attrName + "]");
        }
        ProjectSpace project = new ProjectSpace(DomainObject.newInstance(context, projectId));
        MapList members = project.getMembers(context, HConstants.basicList, rellist, null, null);
        return members;
    }


    /**
     * 获取项目空间下指定角色的成员
     *
     * @param context
     * @param projectId
     * @return
     * @throws MatrixException
     */
    public static MapList getProjectMemberByRole(Context context, String projectId, String role) throws MatrixException
    {
        MapList result = new MapList();

        List<String> relationshipAttrNames = RelationshipUtil.getRelationshipAttrNames(context, Constants.rel_Member);
        StringList rellist = new StringList();
        for (int i = 0; i < relationshipAttrNames.size(); i++)
        {
            String attrName = relationshipAttrNames.get(i);
            rellist.add("attribute[" + attrName + "]");
        }
        ProjectSpace project = new ProjectSpace(DomainObject.newInstance(context, projectId));
        MapList members = project.getMembers(context, HConstants.basicList, rellist, null, null);

        for (int i = 0; i < members.size(); i++)
        {
            Map map_i = (Map) members.get(i);
            String projectRole = (String) map_i.get("attribute[Project Role]");
            if (UIUtil.isNotNullAndNotEmpty(projectRole) && projectRole.equals(role))
            {
                result.add(map_i);
            }
        }
        return result;
    }


    /**
     * 获取项目空间下指定角色的成员
     *
     * @param context
     * @param projectId
     * @return
     * @throws MatrixException
     */
    public static MapList getProjectMemberByAttr(Context context, String projectId, String attr, String value) throws MatrixException
    {
        MapList result = new MapList();

        List<String> relationshipAttrNames = RelationshipUtil.getRelationshipAttrNames(context, Constants.rel_Member);
        StringList rellist = new StringList();
        for (int i = 0; i < relationshipAttrNames.size(); i++)
        {
            String attrName = relationshipAttrNames.get(i);
            rellist.add("attribute[" + attrName + "]");
        }
        ProjectSpace project = new ProjectSpace(DomainObject.newInstance(context, projectId));
        MapList members = project.getMembers(context, HConstants.basicList, rellist, null, null);

        for (int i = 0; i < members.size(); i++)
        {
            Map map_i = (Map) members.get(i);
            String attrValue = (String) map_i.get("attribute[" + attr + "]");
            if (UIUtil.isNotNullAndNotEmpty(attrValue) && attrValue.equals(value))
            {
                result.add(map_i);
            }
        }
        return result;
    }


    /**
     * 获取项目空间下所有的文件夹
     *
     * @param context   上下文
     * @param projectId 项目空间id
     * @param level     获取的层级
     * @return 项目文件夹的集合
     */
    public static MapList getProjectFolders(Context context, String projectId, String level) throws MatrixException
    {
        MapList result = new MapList();
//        String strObjectId =(String) programMap.get("objectId");
//        String strSelectedTable =(String) programMap.get("selectedTable");
//        String strExpandLevel = (String) programMap.get("expandLevel");
        Map argsMap = new HashMap();
        argsMap.put("objectId", projectId);
        argsMap.put("expandLevel", level);
        argsMap.put("selectedTable", "PMCFolderSummary");

        String[] args = JPO.packArgs(argsMap);
        result = JPO.invoke(context, "emxProjectFolder", null, "getTableExpandProjectVaultData", args, MapList.class);

        return result;

    }


    /**
     * 获取项目空间下所有的文件夹
     *
     * @param context   上下文
     * @param projectId 项目空间id
     * @return 项目文件夹的集合
     */
    public static MapList getProjectFolders(Context context, String projectId) throws MatrixException
    {
        return getProjectFolders(context, projectId, "All");
    }


    /**
     * 获取项目空间下所有的文件夹
     *
     * @param context    上下文
     * @param projectObj 项目空间对象
     * @return 项目文件夹的集合
     */
    public static MapList getProjectFolders(Context context, DomainObject projectObj) throws MatrixException
    {
        return getProjectFolders(context, projectObj.getObjectId(context), "All");
    }


    /**
     * 获取项目空间下所有的文件夹
     *
     * @param context    上下文
     * @param projectObj 项目空间对象
     * @return 项目文件夹的集合
     */
    public static MapList getProjectFolders(Context context, DomainObject projectObj, String level) throws MatrixException
    {
        return getProjectFolders(context, projectObj.getObjectId(context), level);
    }


    /**
     * 获取 项目空间下的文件夹 所在的项目空间
     *
     * @param context  上下文
     * @param folderId 文件夹 id
     * @return
     */
    public static Map getFolderProject(Context context, String folderId) throws FrameworkException
    {
        return getFolderProject(context, DomainObject.newInstance(context, folderId));
    }


    /**
     * 获取 项目空间下的文件夹 所在的项目空间
     *
     * @param context   上下文
     * @param folderObj 文件夹对象
     * @return
     */
    public static Map getFolderProject(Context context, DomainObject folderObj) throws FrameworkException
    {

        Map result = new HashMap();


        String strRelationshipPattern = DomainConstants.RELATIONSHIP_PROJECT_VAULTS + "," +
                DomainConstants.RELATIONSHIP_SUB_VAULTS + "," +
                DomainConstants.RELATIONSHIP_VAULTED_OBJECTS_REV2 + "," +
                DomainConstants.RELATIONSHIP_LINKED_FOLDERS;

        String strTypePattern = DomainConstants.QUERY_WILDCARD;

        StringList slRelSelect = new StringList();
        StringList slBusSelect = new StringList();

        //slBusSelect.add(DomainConstants.SELECT_NAME); di7
        slBusSelect.add(DomainConstants.SELECT_ID);
        slBusSelect.add(DomainConstants.SELECT_TYPE);
        slBusSelect.add(DomainConstants.SELECT_NAME);
        slBusSelect.add(DomainConstants.SELECT_REVISION);
        slBusSelect.add("attribute[Title]");

        slRelSelect.add(DomainRelationship.SELECT_ID);
        slRelSelect.add(DomainRelationship.SELECT_FROM_ID);
        slRelSelect.add(DomainRelationship.SELECT_FROM_TYPE);
        slRelSelect.add(DomainRelationship.SELECT_FROM_REVISION);
        slRelSelect.add("from.current");
        slRelSelect.add("from.current.access[fromdisconnect]");

        boolean getFrom = true;
        boolean getTo = false;
        MapList relatedObjects = folderObj.getRelatedObjects(context, strRelationshipPattern, strTypePattern, slBusSelect, slRelSelect, getFrom, getTo, (short) 0, null, null,0);

        for (int i = 0; i < relatedObjects.size(); i++)
        {
            Map map_i = (Map) relatedObjects.get(i);
            String type_i = (String) map_i.get(DomainConstants.SELECT_TYPE);
            if (type_i.equals(DomainConstants.TYPE_PROJECT_SPACE))
            {
                result = map_i;
                break;
            }
        }

        return result;
    }




}
